package com.jtlrm.ckd.util.ContactList;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.sdk.util.ImageUtil;
import com.hyphenate.easeui.utils.EaseUserUtils;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.config.UrlConstants;
import com.jtlrm.ckd.mvp.view.api.SelectInterface;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 选择联系人
 */
public class PicContactsAdapter extends ContactAdapter {
    /**
     * members already in the group
     */
    private Set<SlideBarEntity> existMembers;

    private Set<Integer> cantEditMembers; // 不能编辑的群成员
    SelectInterface selectInterface;

    public PicContactsAdapter(Context context, int resource, List<SlideBarEntity> objects) {
        super(context, resource, objects);
        if (existMembers == null)
            existMembers = new HashSet<>();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (res == 0)
                convertView = layoutInflater.inflate(R.layout.row_contact_with_checkbox, parent, false);
            else
                convertView = layoutInflater.inflate(res, null);
            holder.avatar = convertView.findViewById(R.id.avatar);
            holder.nameView = convertView.findViewById(R.id.name);
            holder.headerView = convertView.findViewById(R.id.header);
            holder.select = convertView.findViewById(R.id.checkbox);
            holder.container = convertView.findViewById(R.id.container);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final SlideBarEntity user = getItem(position);
        if (user == null)
            Log.d("ContactAdapter", position + "");
        final String username = user.getNameStr();
        final String header = user.getInitialLetter();

        if (position == 0 || header != null && !header.equals(getItem(position - 1).getInitialLetter())) {
            if (TextUtils.isEmpty(header)) {
                holder.headerView.setVisibility(View.GONE);
            } else {
                holder.headerView.setVisibility(View.VISIBLE);
                holder.headerView.setText(header);
            }
        } else {
            holder.headerView.setVisibility(View.GONE);
        }
        EaseUserUtils.setUserNick(username, holder.nameView);
        // EaseUserUtils.setUserAvatar(getContext(), username, holder.avatar);
        ImageUtil.loadCircleAvatarImage(holder.avatar, user.getAvatar());
        if (canEdit(position)) {
            // 能够编辑
            holder.select.setEnabled(true);
            holder.container.setEnabled(true);
            holder.select.setButtonDrawable(R.drawable.checkbox_bg_selector);
            if (existMembers.contains(user)) {
                holder.select.setChecked(true);
            } else {
                holder.select.setChecked(false);
            }
        } else {
            holder.select.setEnabled(false);
            holder.container.setEnabled(false);
            holder.select.setButtonDrawable(R.drawable.select_grey);
        }
        holder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.select.isChecked()) {
                    holder.select.setChecked(false);
                    if (existMembers.contains(user)) {
                        existMembers.remove(user);
                    }
                } else {
                    holder.select.setChecked(true);
                    if (!existMembers.contains(user)) {
                        existMembers.add(user);
                    }
                }
                collectSum();
            }
        });
        holder.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 因为顺序的原因
                if (holder.select.isChecked()) {
                    holder.select.setChecked(true);
                    if (!existMembers.contains(user)) {
                        existMembers.add(user);
                    }
                } else {
                    holder.select.setChecked(false);
                    if (existMembers.contains(user)) {
                        existMembers.remove(user);
                    }
                }
                collectSum();
            }
        });
        return convertView;
    }

    /**
     * 判断是否能编辑
     *
     * @param position
     * @return
     */
    public boolean canEdit(int position) {
        if (cantEditMembers != null && cantEditMembers.contains(position)) {
            return false;
        }
        return true;
    }

    private static class ViewHolder {
        CheckBox select;
        ImageView avatar;
        TextView nameView;
        TextView headerView;
        LinearLayout container;
    }

    public void selectAll() {
        if (existMembers != null && userList != null) {
            existMembers.clear();
            for (int i = 0; i < userList.size(); i++) {
                existMembers.add(userList.get(i));
            }
            collectSum();
            notifyDataSetChanged();
        }
    }

    public void clearAll() {
        if (existMembers != null && userList != null) {
            existMembers.clear();
            collectSum();
            notifyDataSetChanged();
        }
    }

    public Set<SlideBarEntity> getExistMembers() {
        return existMembers;
    }

    public void setCantEditMembers(Set<Integer> cantEditMembers) {
        this.cantEditMembers = cantEditMembers;
        collectSum();
        notifyDataSetChanged();
    }

    private void collectSum() {
        if (selectInterface != null) {
            selectInterface.selectResult(existMembers.size());
        }
    }

    public void setSelectInterface(SelectInterface selectInterface) {
        this.selectInterface = selectInterface;
    }

    public void setExistMembers(Set<SlideBarEntity> existMembers) {
        this.existMembers = existMembers;
    }
}
